package com.rrd.dw.mr.gzip.kafka;

import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.third.baihang.BaihangHittedRules;
import com.ucredit.babel.common.model.third.baihang.BaihangRuleResultData;
import com.ucredit.babel.common.model.third.baihang.BaihangRuleResultRecord;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import java.io.IOException;
import java.util.List;

/**
 * 百行-反欺诈规则报告
 *
 * @author guoqiang
 */
public class BaihangRuleResultParse extends ParseBase {
    private Text val = new Text();
    private Text key = new Text();

    /**
     * 百行-反欺诈规则报告
     *
     * @param model
     * @throws InterruptedException
     * @throws IOException
     */
    public boolean parseJob(Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {
        BaihangRuleResultRecord obj = (BaihangRuleResultRecord) model;
        BaihangRuleResultData ruleResult = obj.getRuleResult();
        StringBuilder sb = getBaseInfo(obj, successful);
        sb.append(formatValue(obj.getCode())).append(CRT001);
        sb.append(formatValue(obj.getMessage())).append(CRT001);
        sb.append(formatValue(obj.getErrorCode())).append(CRT001);
        sb.append(formatValue(obj.getErrorMessage())).append(CRT001);
        sb.append(formatValue(ruleResult.getRiskLevel())).append(CRT001);
        sb.append(formatValue(ruleResult.getReasonCode())).append(CRT001);

        List<BaihangHittedRules> rules = ruleResult.getRules();
        StringBuilder TxSb = new StringBuilder(500);
        for(BaihangHittedRules rule: rules) {
            TxSb.delete(0, TxSb.length());
            TxSb.append(formatValue(rule.getName())).append(CRT001);
            TxSb.append(formatValue(rule.getDescription())).append(CRT001);
            TxSb.append(formatValue(rule.getRuleType())).append(CRT001);
            TxSb.append(formatValue(rule.getRiskLevel())).append(CRT001);
            key.set(getKey());
            val.set((sb.toString() + TxSb).replace("\n", " ").replace("\r", " "));
            context.write(key, val);
        }
        return true;
    }

    @Override
    public String getTableName() {
        return "kafka_babel_baihang_ruleresult_inc";
    }

    @Override
    public String getType() {
        return RecordType.BAIHANG_RULERESULT_RECORD.toString();
    }

    @Override
    public String getComment() {
        return "百行-反欺诈规则报告";
    }
}
